package com.uzhie.web.module;

import com.uzhie.exception.WarningException;
import com.uzhie.jt808.msg.entity.JT_0200;
import com.uzhie.jt808.msg.entity.JT_200.Media_Fatigue;
import com.uzhie.jt808.msg.entity.JT_200.Media_Warn;
import com.uzhie.util.ConstantKit;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.*;


/**
 * 报警模块弹窗显示
 * create by lilongfei
 */
@Table(name="mjm_warning")
public class WarningToPop implements Serializable,Cloneable{
    /** 警情id */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotEmpty(message = WarningException.Status.ID_ISNULL)
    private String id ;
    /** 警情编码 */
    private String code ;
    /** 部门ID */
    @NotEmpty(message = WarningException.Status.DEPART_ERROR)
    private String departId ;
    /** 部门名称 */
    @NotEmpty(message = WarningException.Status.DEPART_ERROR)
    private String departName ;
    /** 车牌号 */
    private String vehicleNumber ;
    /**
     * 报警类别
     */
    private String category;
    /**
     * 内置sim卡
     */
    @NotEmpty(message = WarningException.Status.SIM_ERROR)
    private String simNumber;
    /** 车辆类型 */
    private String vehicleType ;
    /** 当前速度 */
    private String speed ;
    /** 定位时间 */
    private String positionTime ;
    /** 行停时长 */
    private Double positionDuration ;
    /** 联系电话 */
    private String telephone ;
    /** 驾驶员 */
    private String driver ;
    /** 报警类型 */
    private String type ;
    /** 上报时间 */
    private Date reportTime ;
    /** （经度） */
    private Double lon ;
    /** （纬度） */
    private Double lat ;
    /** 位置信息 */
    private String address ;
    /** 警情状态 */
    // 0 删除 1 未处理 2 已处理 3 误报 4 忽略
    private Integer status ;
    /** 备注 */
    private String remark ;
    /** 处理人id */
    private String handlerId ;
    /** 处理人姓名 */
    private String handlerName ;
    /** 处理时间 */
    private Date handlerTime ;

    /** 警情id */
    public String getId(){
        return this.id;
    }
    /** 警情id */
    public void setId(String id){
        this.id = id;
    }
    /** 警情编码 */
    public String getCode(){
        return this.code;
    }
    /** 警情编码 */
    public void setCode(String code){
        this.code = code;
    }
    /** 部门ID */
    public String getDepartId(){
        return this.departId;
    }
    /** 部门ID */
    public void setDepartId(String departId){
        this.departId = departId;
    }
    /** 部门名称 */
    public String getDepartName(){
        return this.departName;
    }
    /** 部门名称 */
    public void setDepartName(String departName){
        this.departName = departName;
    }
    /** 车牌号 */
    public String getVehicleNumber(){
        return this.vehicleNumber;
    }
    /** 车牌号 */
    public void setVehicleNumber(String vehicleNumber){
        this.vehicleNumber = vehicleNumber;
    }
    /** 车辆类型 */
    public String getVehicleType(){
        return this.vehicleType;
    }
    /** 车辆类型 */
    public void setVehicleType(String vehicleType){
        this.vehicleType = vehicleType;
    }
    /** 当前速度 */
    public String getSpeed(){
        return this.speed;
    }
    /** 当前速度 */
    public void setSpeed(String speed){
        this.speed = speed;
    }
    /** 定位时间 */
    public String getPositionTime(){
        return this.positionTime;
    }
    /** 定位时间 */
    public void setPositionTime(String positionTime){
        this.positionTime = positionTime;
    }
    /** 行停时长 */
    public Double getPositionDuration(){
        return this.positionDuration;
    }
    /** 行停时长 */
    public void setPositionDuration(Double positionDuration){
        this.positionDuration = positionDuration;
    }
    /** 联系电话 */
    public String getTelephone(){
        return this.telephone;
    }
    /** 联系电话 */
    public void setTelephone(String telephone){
        this.telephone = telephone;
    }
    /** 驾驶员 */
    public String getDriver(){
        return this.driver;
    }
    /** 驾驶员 */
    public void setDriver(String driver){
        this.driver = driver;
    }
    /** 报警类型 */
    public String getType(){
        return this.type;
    }
    /** 报警类型 */
    public void setType(String type){
        this.type = type;
    }
    /** 上报时间 */
    public Date getReportTime(){
        return this.reportTime;
    }
    /** 上报时间 */
    public void setReportTime(Date reportTime){
        this.reportTime = reportTime;
    }

    public Double getLon() {
        return lon;
    }

    public void setLon(Double lon) {
        this.lon = lon;
    }

    public Double getLat() {
        return lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    /** 位置信息 */
    public String getAddress(){
        return this.address;
    }
    /** 位置信息 */
    public void setAddress(String address){
        this.address = address;
    }
    /** 警情状态 */
    public Integer getStatus(){
        return this.status;
    }
    /** 警情状态 */
    public void setStatus(Integer status){
        this.status = status;
    }
    /** 备注 */
    public String getRemark(){
        return this.remark;
    }
    /** 备注 */
    public void setRemark(String remark){
        this.remark = remark;
    }
    /** 处理人id */
    public String getHandlerId(){
        return this.handlerId;
    }
    /** 处理人id */
    public void setHandlerId(String handlerId){
        this.handlerId = handlerId;
    }
    /** 处理人姓名 */
    public String getHandlerName(){
        return this.handlerName;
    }
    /** 处理人姓名 */
    public void setHandlerName(String handlerName){
        this.handlerName = handlerName;
    }
    /** 处理时间 */
    public Date getHandlerTime(){
        return this.handlerTime;
    }
    /** 处理时间 */
    public void setHandlerTime(Date handlerTime){
        this.handlerTime = handlerTime;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getSimNumber() {
        return simNumber;
    }

    public void setSimNumber(String simNumber) {
        this.simNumber = simNumber;
    }



    public static List<WarningToPop> ReadFromOrder(JT_0200 order, Vehicle vehicle, String driver){

        List<WarningToPop> warnings = new ArrayList<>();
        Media_Warn media_warn =  order.getMediaWarn();
        Map<Integer,Integer> status =  media_warn.getStatus();
        //把报警类型分割开
        Iterator<Map.Entry<Integer,Integer>> iterator = status.entrySet().iterator();
        //如果当前存在视频报警类型
        while (iterator.hasNext()){
            Map.Entry<Integer,Integer> entry = iterator.next();
            WarningToPop warning = new WarningToPop();
            warning.setDepartId(vehicle.getDepartId());
            warning.setDepartName(vehicle.getDepartName());
            warning.setVehicleNumber(vehicle.getNumber());
            warning.setSimNumber(vehicle.getSimNumber());
            warning.setVehicleType(vehicle.getType());
            //媒体设备警情类型
            warning.setCategory(entry.getKey()+ "");
            //拼装异常行为告警(文字)
            if (entry.getKey() == ConstantKit.MEDIAWARN.SPECIAL.getType()) {
                StringBuffer stringBuffer = new StringBuffer() ;
                //获取异常告警行为的具体分类,疲劳,抽烟,打电话
                Media_Fatigue media_fatigue =  order.getMediaFatigue();
                if (media_fatigue == null) continue;
                Iterator<Map.Entry<Integer,Integer>> fatigueIter = media_fatigue.getStatus().entrySet().iterator();
                //判断当前存在什么异常行为
                while (fatigueIter.hasNext()){
                    Map.Entry<Integer,Integer> entry1 = fatigueIter.next();
                    for (ConstantKit.MEDIAFATIGURE type : ConstantKit.MEDIAFATIGURE.values()) {
                        if (type.getType() == entry1.getKey()){
                            stringBuffer.append(type.getName() + ",");
                        }
                    }
                }
                stringBuffer.delete(stringBuffer.length()-1,stringBuffer.length());
                warning.setType(stringBuffer.toString());
            }
            //拼装普通视频告警(文字)
            else {
                for (ConstantKit.MEDIAWARN type : ConstantKit.MEDIAWARN.values()) {
                   if (type.getType() == entry.getKey())
                        warning.setType(type.getName());
                }
            }
            warning.setSpeed(order.getSpeed() + "");
            warning.setPositionTime(order.getTime());
            warning.setLat(order.getLatitude()/1000000d);
            warning.setLon(order.getLongitude()/1000000d);
            warning.setDriver(driver);
            warning.setTelephone(vehicle.getTelephone());
            warning.setStatus(ConstantKit.WARNSTATUS.PENDING.getType());
            warning.setReportTime(new Date());
            warnings.add(warning);

        }
        return warnings;
    }
}